package com.schoolgard.action.alarm;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;

import com.schoolegard.services.AlarmLogService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.AlarmClass;
import com.schoolgard.entitybean.AlarmLog;
import com.schoolgard.entitybean.GridDataModel;
import com.schoolgard.entitybean.HdState;
import com.schoolgard.entitybean.Users;
import com.supersystem.utils.DateJsonValueProcessor;
import com.supersystem.utils.DateUtil;
import com.supersystem.utils.TypeUtils;

public class AlarmLogAction extends BaseAction {

	private AlarmLogService alarmLogService;
	
	private AlarmLog alarmLog;

	public void findAll() {
		try{
		String startStr = request.getParameter("start"); // grid传过来的当前数据条数
		String limitStr = request.getParameter("limit"); // 每页显示的数据数

		int start = Integer.parseInt(startStr);
		int limit = Integer.parseInt(limitStr);
		if (limit == 0) {
			limit = Integer.MAX_VALUE;
		}
		GridDataModel<AlarmLog> page = alarmLogService.find4Page(constructQueryParam(),(Users)getSession().get(TypeUtils.USER),start, limit);

		JsonValueProcessor jsonProcessor = new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config = new JsonConfig(); 
		config.registerJsonValueProcessor(Date.class, jsonProcessor);
		
		String jsonStr = JSONObject.fromObject(page,config).toString();
		writeToResponse(jsonStr);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void confirm() {
		String id = request.getParameter("alarmLogId");
		writeToResponse(updateStatus(id, "2"));
	}

	public void close() {
		String id = request.getParameter("alarmLogId");
		writeToResponse(updateStatus(id, "-1"));
	}

	public void deal(){
		String id = request.getParameter("alarmLogId");
		if (StringUtils.isBlank(id)) {
			writeToResponse("0");
			return;
		}
		Users user = (Users)getSession().get(TypeUtils.USER);
		if(user == null) {
			return;
		}
		writeToResponse(alarmLogService.deal(Long.valueOf(id),user) ? "1" : "0");
	}
	
	private String updateStatus(String id, String status) {
		if (StringUtils.isBlank(id)) {
			return "0";
		}
		try {
			Users user = (Users)getSession().get(TypeUtils.USER);
			AlarmLog alarmLog = alarmLogService.findById(Long.valueOf(id));
			if (alarmLog == null || user == null) {
				return "0";
			}
			alarmLog.setStatus(status);
			alarmLog.setUserId(user.getId());
			alarmLog.setUserName(user.getName());
			alarmLog.setConfirmDatetime(new Date());
			alarmLogService.update(alarmLog);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	public void setAlarmLogService(AlarmLogService alarmLogService) {
		this.alarmLogService = alarmLogService;
	}
	
	private AlarmLog constructQueryParam() throws ParseException{
		//查询条件
		String status = request.getParameter("alarmLogStatus");
		String id = request.getParameter("deviceId");
		String alarmClassType = request.getParameter("alarmClassType");
		String alarmClassLevel = request.getParameter("alarmClassLevel");
		String time = request.getParameter("alarmLogTime");
		if(alarmLog == null){
			alarmLog = new AlarmLog();
		}
		AlarmClass alarmClass = new AlarmClass();
		if(StringUtils.isEmpty(alarmClassType)) {
			alarmClass.setType(null);
		}else{
			alarmClass.setType(Integer.valueOf(alarmClassType));
		}
		if(StringUtils.isEmpty(alarmClassLevel)){
			alarmClassLevel = "1";
		}else if("0".equals(alarmClassLevel)){
			alarmClassLevel = null;
		}
		alarmClass.setLevel(alarmClassLevel);
		if(StringUtils.isEmpty(status)){
			status = "1";
		}else if("0".equals(status)) {
			status = null;
		}
		alarmLog.setStatus(status);
		alarmLog.setAlarmClass(alarmClass);
		alarmLog.setUserName(id);//暂时用这个字段传递
		Date date = DateUtil.parseDate(DateUtil.format(new Date()));
		if("9".equals(time)){
			alarmLog.setDatetime(null);
			alarmLog.setConfirmDatetime(null);
		}else if("1".equals(time)){
			alarmLog.setConfirmDatetime(DateUtils.addDays(date,-7));
		}else{
			alarmLog.setDatetime(DateUtils.addDays(date, -7));
			alarmLog.setConfirmDatetime(date);
		}
		return alarmLog;
	}

	public void findTotalList(){
		try{
		String startStr = request.getParameter("start"); // grid传过来的当前数据条数
		String limitStr = request.getParameter("limit"); // 每页显示的数据数
		String id = request.getParameter("totalId");
		Date date = DateUtil.parseDate(DateUtil.format(new Date()));
		if(alarmLog == null){
			alarmLog = new AlarmLog();
		}
		alarmLog.setDatetime(date);
		alarmLog.setUserName(id);
		alarmLog.setStatus("1");
		
		int start = Integer.parseInt(startStr);
		int limit = Integer.parseInt(limitStr);
		if (limit == 0) {
			limit = Integer.MAX_VALUE;
		}
		GridDataModel<AlarmLog> page = alarmLogService.find4Page(alarmLog,(Users)getSession().get(TypeUtils.USER),start, limit);

		JsonValueProcessor jsonProcessor = new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config = new JsonConfig(); 
		config.registerJsonValueProcessor(Date.class, jsonProcessor);
		
		String jsonStr = JSONObject.fromObject(page,config).toString();
		writeToResponse(jsonStr);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void countError() {
		try{
		String id = request.getParameter("totalId");
		Date date = DateUtil.parseDate(DateUtil.format(new Date()));
		if(alarmLog == null){
			alarmLog = new AlarmLog();
		}
		alarmLog.setDatetime(date);
		alarmLog.setUserName(id);
		alarmLog.setStatus("1");
		List<Integer> counts =  alarmLogService.countError(alarmLog, (Users)getSession().get(TypeUtils.USER));
		JSONObject json = new JSONObject();
		for(int j = 0;j<counts.size();j++){
			json.put("level"+j, counts.get(j));
		}
		writeToResponse(json.toString());
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void findHdState(){
		String id = request.getParameter("totalId");
		HdState hs = alarmLogService.findByGuid(id);
		writeToResponse(JSONObject.fromObject(hs).toString());
	}
	
	public AlarmLog getAlarmLog() {
		return alarmLog;
	}

	public void setAlarmLog(AlarmLog alarmLog) {
		this.alarmLog = alarmLog;
	}
	
}
